﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace firstForm
{
    public partial class byteHandler : Form
    {
        string byteMaskHolder;
        public byteHandler()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            //clear messages box
            messages.Text = "";

            if (decToHex.Checked)
            {
               
                if (byteMask.Text == "")
                {
                    string temp = decim.Text;
                    hexi.Text = Conversions.Decimal.toHex(temp);
                    bits.Text = Conversions.Hex.toBinary(hexi.Text);
                    if (!(hexi.Text == "Invalid number in decimal field"))
                    {
                        lengthBytes.Text = Conversions.Decimal.howManyBytes(temp);
                    }
                    else
                    {
                        hexi.Text = "";
                        messages.Text = "Invalid number in decimal field";
                        
                    }
                }
                else
                {

                    string temp = decim.Text;
                    int byteMaskNum = Convert.ToInt32(byteMask.Text);
                    hexi.Text = Conversions.Decimal.toHex(temp, byteMaskNum * 2);
                    bits.Text = Conversions.Hex.toBinary(hexi.Text, byteMaskNum * 4);
                    if (!(hexi.Text == "Invalid number in decimal field"))
                    {
                        lengthBytes.Text = Conversions.Decimal.howManyBytes(temp);
                    }
                    else
                    {
                        messages.Text = "Invalid number in decimal field";
                    }
                   
                    
                }
            }
            else
            {
               
                string temp = hexi.Text;
                decim.Text = Conversions.Hex.toDecimal(temp);
                bits.Text = Conversions.Hex.toBinary(temp);
                if (decim.Text == "Contains invalid hex characters")
                {
                    decim.Text = "";
                    messages.Text = "Contains invalid hex characters";
                    
                }
                else
                {
                    lengthBytes.Text = Conversions.Hex.howManyBytes(temp);
                   
                }
                if (byteMask.Text != "")
                {
                    //TODO Add byte mask to hex number when converting to decimal?
                }
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new aboutBox().Show();
        }

        private void decToHex_CheckedChanged(object sender, EventArgs e)
        {
            if (decToHex.Checked)
            {
                byteMask.ReadOnly = false;
                byteMask.Text = byteMaskHolder;
            }
            else
            {
                byteMask.ReadOnly = true;
                byteMaskHolder =  byteMask.Text;
                byteMask.Text = "N/A";
            }
        }


        private void decim_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                decToHex.Checked = true;
                this.button1.PerformClick();
                
            }
        }

        private void hexi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                hextoDec.Checked = true;
                this.button1.PerformClick();
                
            }
        }

      

 

 
    


    }
}
